package com.dht.imagepicker

import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.RecyclerView

/**
 * @author dht
 *
 * @date 2023/12/27 18:12
 **/
class GridItemDecor : RecyclerView.ItemDecoration() {

    companion object {

        private const val DEFAULT_SPAN_COUNT = 4

        private const val DEFAULT_SPACING = 2
    }

    /**
     * 绘制分割线
     */
    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        super.getItemOffsets(outRect, view, parent, state)

        val position = parent.getChildAdapterPosition(view)
        if (position < DEFAULT_SPAN_COUNT) {
            outRect.top = 0
        } else {
            outRect.top = DEFAULT_SPACING
        }

        if (position % DEFAULT_SPAN_COUNT == 0) {
            outRect.left = 0
        } else {
            outRect.left = DEFAULT_SPACING
        }
    }
}